/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-1-9 下午02:46:32
 *
 * PublishProjectAction.java 
 *
 *******************************************************************/
package com.transer.web.action.passport;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.transer.constant.Constants;
import com.transer.engine.search.WordAnalyzer;
import com.transer.persistance.bean.Project;
import com.transer.persistance.bean.User;
import com.transer.persistance.dao.IProjectDao;
import com.transer.utils.FileUtils;
import com.transer.utils.Utils;
import com.transer.web.action.TranserActionSupport;
import com.transer.web.exception.NotLoginException;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class PublishProjectAction extends TranserActionSupport {
	private static final Logger logger = Logger.getLogger(PublishProjectAction.class);
	private static final long serialVersionUID = -7432576120238353191L;
	/** 项目操作 */
	private IProjectDao projectDao;
	/** 项目 */
	private Project project;
	/** 竞标时长 */
	private int period;
	// 文件
	private File projectdoc;
	// 文件的内容类型
	private String projectdocContentType;
	// 上传文件名
	private String projectdocFileName;
	//
	private String invoice;
	// 关键字
	private String keyWords;
	// 翻译类型
	private String goodAt;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		try {
			// 查看是否登陆
			User user = (User) ActionContext.getContext().getSession().get(Constants.SESSION_USER);
			// 没登陆
			if (user == null) {
				throw new NotLoginException();
			}

			if (projectdoc != null) {
				// 后缀名
				// String suffix =
				// projectdocFileName.substring(projectdocFileName.indexOf("."));
				// 根路径
				String rootPath = ServletActionContext.getServletContext().getRealPath("/");
				// 保存证书
				FileUtils.saveAs(projectdoc, rootPath + "/doc/" + user.getId(), projectdocFileName);
				// 证书路径
				project.setDoc("/doc/" + user.getId() + "/" + projectdocFileName);
			}
			// 发布人
			project.setUserID(user.getId());
			// 是否需要发票
			project.setInvoice(Utils.getInteger((invoice)) == 1);
			// 开始竞标
			project.setStart(new Date());
			// 竞标结束日期
			project.setEnd(com.transer.utils.DateUtils.parseDate(DateUtils.addDays(project.getStart(), period + 1), com.transer.utils.DateUtils.FORMAT_YYYY_MM_DD));
			// 翻译类型
			project.setTranslateType(goodAt);
			// 构建关键字
			project.setKeyWords(WordAnalyzer.encodeChinese(project.getKeyWords() + " " + project.getTitle() + " " + project.getDesc()));
			/** 提取关键字 */
			projectDao.addProject(project);
		} catch (Exception e) {
			logger.error("发布项目出错", e);
			return TranserActionSupport.INPUT;
		}
		// 成功
		return TranserActionSupport.SUCCESS;
	}

	/**
	 * @return the projectDao
	 */
	public IProjectDao getProjectDao() {
		return projectDao;
	}

	/**
	 * @param projectDao
	 *            the projectDao to set
	 */
	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	/**
	 * @return the project
	 */
	public Project getProject() {
		return project;
	}

	/**
	 * @param project
	 *            the project to set
	 */
	public void setProject(Project project) {
		this.project = project;
	}

	/**
	 * @return the period
	 */
	public int getPeriod() {
		return period;
	}

	/**
	 * @param period
	 *            the period to set
	 */
	public void setPeriod(int period) {
		this.period = period;
	}

	/**
	 * @return the projectdoc
	 */
	public File getProjectdoc() {
		return projectdoc;
	}

	/**
	 * @param projectdoc
	 *            the projectdoc to set
	 */
	public void setProjectdoc(File projectdoc) {
		this.projectdoc = projectdoc;
	}

	/**
	 * @return the projectdocContentType
	 */
	public String getProjectdocContentType() {
		return projectdocContentType;
	}

	/**
	 * @param projectdocContentType
	 *            the projectdocContentType to set
	 */
	public void setProjectdocContentType(String projectdocContentType) {
		this.projectdocContentType = projectdocContentType;
	}

	/**
	 * @return the projectdocFileName
	 */
	public String getProjectdocFileName() {
		return projectdocFileName;
	}

	/**
	 * @param projectdocFileName
	 *            the projectdocFileName to set
	 */
	public void setProjectdocFileName(String projectdocFileName) {
		this.projectdocFileName = projectdocFileName;
	}

	/**
	 * @return the invoice
	 */
	public String getInvoice() {
		return invoice;
	}

	/**
	 * @param invoice
	 *            the invoice to set
	 */
	public void setInvoice(String invoice) {
		this.invoice = invoice;
	}

	/**
	 * @return the keyWords
	 */
	public String getKeyWords() {
		return keyWords;
	}

	/**
	 * @param keyWords
	 *            the keyWords to set
	 */
	public void setKeyWords(String keyWords) {
		this.keyWords = keyWords;
	}

	/**
	 * @return the goodAt
	 */
	public String getGoodAt() {
		return goodAt;
	}

	/**
	 * @param goodAt
	 *            the goodAt to set
	 */
	public void setGoodAt(String goodAt) {
		this.goodAt = goodAt;
	}
}